
class Obstacle 
{
	//used for storing actual layout of obstacle
	private var _obstacleLayout:Array;
	private var _obstacleModel:GameObject;
	private var _positionIndex:Vector2;
	private var _size:Vector2;
	private var _visible:boolean;
	private var _revealCount:int = 1;
	
	public static var HIT_CHANCE_REDUCTION:int = 20;
	
	//used to store the reference to the tiles it is on
	private var _tileList:Array;
	
	public function Obstacle(target:Obstacle)
	{
		clone(target);
		_positionIndex = new Vector2();
		_tileList = new Array();
		_visible = true;
	}
	
	public function Obstacle(layout:Array, size:Vector2, model:GameObject)
	{
		_obstacleLayout = layout;
		_obstacleModel = model;
		_size = size;
		_positionIndex = new Vector2();
		_tileList = new Array();
	}
	
	//anchor position should be the middle of the layout. Ceiling(width/2.0)
	//Eg 5x5 layout. position should be at [3,3]
	//Eg 4x5 layout. position should be at [2,3]
	//therefore when setting position, please take note of the anchor position of the obstacle
	public function setPosition(x:int, z:int):void
	{
		_positionIndex.x = x;
		_positionIndex.y = z;
	}
	
	public function setWorldCoord(x:float, y:float, z:float):void
	{
		_obstacleModel.transform.position.x = x;
		_obstacleModel.transform.position.y = y;
		_obstacleModel.transform.position.z = z;
	}
	
	public function addTile(t:Tile):void 
	{
		_tileList.Push(t);
	}
	
	public function getLayout():Array { return _obstacleLayout; }
	public function getModel():GameObject { return _obstacleModel; }
	
	public function getLayoutSize():Vector2 { return _size; }
	
	public function clone(target:Obstacle):void
	{
		_obstacleLayout = target.getLayout();
		_size = target.getLayoutSize();
		_obstacleModel = MonoBehaviour.Instantiate(target.getModel());
		
		if (_obstacleLayout.length == 1)
			_obstacleModel.transform.rotation = Quaternion.AngleAxis(90*Random.Range(0,4), Vector3.up);
		
	}
	
	public function isVisible():boolean { return _visible;}
	public function setIsVisible(b:boolean):void 
	{ 
		if (!_visible && b)
			_revealCount++;
		else if (_visible && b)
			_revealCount++;
		else if (_visible && !b)
			_revealCount--;
		
		var renderers;
		if (_revealCount > 0 && !_visible)
		{
			renderers = _obstacleModel.GetComponentsInChildren(Renderer);
			for (var r : Renderer in renderers)
    			r.enabled = true;
			_visible = true;
		}
		else if (_revealCount <= 0 && _visible)
		{	
			renderers = _obstacleModel.GetComponentsInChildren(Renderer);
			for (var r : Renderer in renderers)
    			r.enabled = false;
				
			_visible = false;
			_revealCount = 0;
		}
	}
}
